package com.load.filter;


import com.utils.IpUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 跨域设置
 */
@WebFilter
public class CrossOriginFilter implements Filter {
    private FilterConfig config = null;

    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }

    @Override
    public void destroy() {
        this.config = null;
    }

    /**
     * @comment 跨域的设置
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        // 表明它允许"http://xxx"发起跨域请求
        String AccessControlAllowOrigin= "http://"+ IpUtil.getIpAddress(httpRequest)+":8080";
        httpResponse.setHeader("Access-Control-Allow-Origin",
                "*");

        // 表明在xxx秒内，不需要再发送预检验请求，可以缓存该结果
        String AccessControlAllowMethods="POST, GET, DELETE, PUT";
        httpResponse.setHeader("Access-Control-Allow-Methods",
                "*");
        httpResponse.setHeader("Access-Control-Allow-Credentials", "true");

        // 表明它允许xxx的外域请求
//        String AccessControlMaxAge="3628800";
//        httpResponse.setHeader("Access-Control-Max-Age",
//                AccessControlMaxAge);

        // 表明它允许跨域请求包含xxx头
        String AccessControlAllowHeaders="x-requested-with,Content-Type,X-token";
        httpResponse.setHeader("Access-Control-Allow-Headers",
                "*");

        //无关Cookie跨域Ajax请求
//        String AccessControlAllowCredentials ="true";
//        httpResponse.setHeader("Access-Control-Allow-Credentials",
//                AccessControlAllowCredentials);

        /**以下为正式服配置**/
        // 表明它允许"http://xxx"发起跨域请求
//        String AccessControlAllowOrigin= "https://www.woye.vip";
//        httpResponse.setHeader("Access-Control-Allow-Origin",
//                AccessControlAllowOrigin);
//
//        // 表明在xxx秒内，不需要再发送预检验请求，可以缓存该结果
//        String AccessControlAllowMethods="POST,GET,DELETE,PUT,OPTIONS";
//        httpResponse.setHeader("Access-Control-Allow-Methods",
//                AccessControlAllowMethods);
//
//        // 表明它允许xxx的外域请求
//        String AccessControlMaxAge="3628800";
//        httpResponse.setHeader("Access-Control-Max-Age",
//                AccessControlMaxAge);
//
//        // 表明它允许跨域请求包含xxx头
//        String AccessControlAllowHeaders="x-requested-with,Content-Type,X-token";
//        httpResponse.setHeader("Access-Control-Allow-Headers",
//                AccessControlAllowHeaders);
//
//        //无关Cookie跨域Ajax请求
//        String AccessControlAllowCredentials ="true";
//        httpResponse.setHeader("Access-Control-Allow-Credentials",
//                AccessControlAllowCredentials);

        chain.doFilter(request, response);
    }

}